/**
 * 
 *  @project Air Traff Monitor
 * 
 *  @author M.Emre ATASEVER <m.emre.atas@gmail.com>
 *  @author M.Burak ALKAN <mburakalkan@gmail.com>
 *  @author H.Burak KARAMIK <bkaramik@gmail.com>
 *
 *	`Air Traffic Monitor' is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "statmodel.h"

StatusModel::StatusModel(QKismetSocket *sock) : KismetDataModel<StatusRootItem,StatusItem>(sock) 
{
	connect(socket,SIGNAL(statInfo(QString)),this,SLOT(statInfo(QString)));
}

QVariant StatusModel::data(const QModelIndex &index, int role) const
{
	if(!index.isValid())
		return QVariant();
	if(role != Qt::DisplayRole)
		return QVariant();

	StatusItem *item	= static_cast<StatusItem*>(index.internalPointer());
	return item->data(index.column());
}

QVariant StatusModel::headerData(int, Qt::Orientation,int) const
{
	return QVariant();
}

QModelIndex StatusModel::index(int row, int column, const QModelIndex &parent) const
{
	if(!hasIndex(row,column,parent))
		return QModelIndex();
	
	
	StatusItem *it	= rootItem->items[row];
	if(it)
		return createIndex(row,column,it);
	else
		return QModelIndex();
}

QModelIndex StatusModel::parent(const QModelIndex &index) const
{
	if(index.isValid())
		return QModelIndex();
	
	return QModelIndex();
}

int StatusModel::columnCount(const QModelIndex&) const
{
	return 2;
}

void StatusModel::statInfo(QString s)
{
	int c	= rootItem->items.size();
	
	beginInsertRows(QModelIndex(),c,c);
	
	StatusItem *item	= new StatusItem(s,rootItem);
	rootItem->items.append(item);
	
	endInsertRows();
}

//StatusItem implementation

StatusItem::StatusItem(QString s, StatusRootItem *parent)
{
	root	= parent;

	time	= QDateTime::currentDateTime();
	msg	= s;
}

StatusItem::~StatusItem()
{
	//TODO: clean clients
}

QVariant StatusItem::data(int column)
{
	if(column == 0)
		return time.toString();
	else if(column == 1)
		return msg;
	else return QVariant();
}

StatusRootItem* StatusItem::parent()
{
	return root;
}

int StatusItem::row()
{
	return root->items.indexOf(this);
}
